package com.fw.service.interfacevalidator.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;

import org.springframework.beans.factory.annotation.Autowired;

import com.fw.service.inboundmessagelog.bean.InboundMessageLogBean;
import com.fw.service.interfacevalidator.InterfaceValidatorService;
import com.fw.service.message.MessageService;
import com.fw.service.persistence.MerchantProfilePersistenceService;
import com.fw.service.persistence.ServiceProfilePersistenceService;
import com.fw.service.persistence.dto.Error;
import com.fw.service.persistence.dto.MerchantProfile;
import com.fw.service.persistence.dto.ServiceProfile;

public class InterfaceValidatorServiceImpl implements InterfaceValidatorService {

    @Autowired
    private MerchantProfilePersistenceService merchantProfilePersistenceService;

    @Autowired
    private ServiceProfilePersistenceService serviceProfilePersistenceService;

    @Autowired
    private MessageService messageService;

    @Override
    public List<Error> validate(InboundMessageLogBean inbondMessageLogBean) {
	List<Error> errors = new ArrayList<Error>();
	/**
	 * 1.Validate
	 */
	String merchantId = inbondMessageLogBean.getMerchantID();
	if (merchantId != null) {
	    List<MerchantProfile> merchantProfiles = merchantProfilePersistenceService.selectByMerchantIdAndStatus(merchantId, null);
	    if (merchantProfiles == null || merchantProfiles.isEmpty()) {
		Error error = new Error();
		error.setErrorCode(messageService.getMessage("com.fw.service.interfacevalidator.validate.merchantID.notfound", null));
		errors.add(error);
	    }
	}
	String serviceId = inbondMessageLogBean.getServiceID();
	if (serviceId != null) {
	    List<ServiceProfile> serviceProfiles = serviceProfilePersistenceService.selectByServiceIdAndStatus(serviceId, null);
	    if (serviceProfiles == null || serviceProfiles.isEmpty()) {
		Error error = new Error();
		error.setErrorCode(messageService.getMessage("com.fw.service.interfacevalidator.validate.serviceID.notfound", null));
		errors.add(error);
	    }
	}

	String currencyCode = inbondMessageLogBean.getCurrencyCode();
	if (currencyCode != null && !"764".equals(currencyCode)) {
	    Error error = new Error();
	    error.setErrorCode(messageService.getMessage("com.fw.service.interfacevalidator.validate.currencyCode.invalid", null));
	    errors.add(error);
	}

	String language = inbondMessageLogBean.getLanguage();
	if (language != null && !"TH".equals(language) && !"EN".equals(language) && !"JA".equals(language)) {
	    Error error = new Error();
	    error.setErrorCode(messageService.getMessage("com.fw.service.interfacevalidator.validate.language.invalid", null));
	    errors.add(error);
	}

	String typeOfCreditCard = inbondMessageLogBean.getTypeOfCreditCard();
	if (typeOfCreditCard != null && !"0".equals(typeOfCreditCard) && !"1".equals(typeOfCreditCard) && !"2".equals(typeOfCreditCard)
		&& !"3".equals(typeOfCreditCard) && !"4".equals(typeOfCreditCard)) {
	    Error error = new Error();
	    error.setErrorCode(messageService.getMessage("com.fw.service.interfacevalidator.validate.typeOfCreditCard.invalid", null));
	    errors.add(error);
	}

	String enableSkippage = inbondMessageLogBean.getEnableSkippage();
	if (enableSkippage != null && !"T".equals(enableSkippage) && !"F".equals(enableSkippage)) {
	    Error error = new Error();
	    error.setErrorCode(messageService.getMessage("com.fw.service.interfacevalidator.validate.enableSkippage.invalid", null));
	    errors.add(error);
	}

	ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
	Validator validator = factory.getValidator();
	Set<ConstraintViolation<InboundMessageLogBean>> constraintViolations = validator.validate(inbondMessageLogBean);
	if (constraintViolations.size() > 0) {
	    for (ConstraintViolation<InboundMessageLogBean> constraint : constraintViolations) {
		Error error = new Error();
		error.setErrorCode(constraint.getMessage());
		errors.add(error);
	    }
	}
	return errors;
    }

}
